<template>
    <div class="app">
        <div>
        <div class="top-banner">
            <img :src="navbar">
        </div> 
        <div class="title-line">
          <span class="tit" style="font-size: 38px;">登录</span>
        </div>
        </div>
        <!-- 用户登录表单 -->
        <el-form ref="form" :model="form" label-width="80px" class="loginForm">
            <el-form-item
            prop="username"
            :rules="[
                  { required: true, message: '请输入用户名', trigger: 'blur' },
                  { pattern: /\d|\w|\W/, message: '请输入正确的用户名', trigger: 'blur' }
                    ]"
            >
              <el-input v-model="form.username" placeholder="您的用户名"></el-input>
            </el-form-item>
            <el-form-item 
            prop="password"
            :rules="[
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { pattern: /\d|\w/, message: '请输入正确的密码', trigger: 'blur' }
                    ]"
            >
              <el-input v-model="form.password" type="password" placeholder="您的密码"></el-input>
            </el-form-item>
            <el-form-item  prop="checkCode"
                       :rules="[
                              { required: true, message: '请输入验证码', trigger: 'blur' },
                              { pattern: /\d|\w/, message: '请输入正确的验证码', trigger: 'blur' }
                       ]"
            >
                      <el-input v-model="form.checkCode" placeholder="请输入验证码">
                      </el-input>
                      <img :src="captcha" @click="getCaptcha" class="inline-block identifyCaptcha"/>
                      <div class="remember">
                          <label>
                              <input type="checkbox">
						记住我
						
                              <span>不是自己的电脑上不要勾选此项</span>
                          </label>
                      </div>
            </el-form-item> 
            <el-form-item>
            <center>
             <el-button type="primary" @click="submitForm('form')" style="width:152px">登录</el-button>
             <el-button  style="width:152px;" @click="register">注册
             </el-button>
            </center>
             </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {apiurl} from "../../service/api.js";
import background from '../../assets/navbar1.png'

export default {
    name : 'register',
    created() {     
        //获取验证码
        this.getCaptcha();
        //查看是否保存本地信息
        var remember = JSON.parse(localStorage.getItem("remember"))
        if (remember!=null){
            this.form.username = remember.username
            this.form.password = remember.password
        }
    },
    data(){
        return{
            navbar : background,  //顶部背景栏
            //获取验证码所需提交的表单
            captchaform: {
                    CaptchaType: "character",
                    Id: '',
                    VerifyValue: '',
                    ConfigAudio: {
                        CaptchaLen: 6,
                        Language: 'zh'
                    },
                    ConfigCharacter: {
                        Height: 60,
                        Width: 240,
                        Mode: 2,
                        ComplexOfNoiseText: 0,
                        ComplexOfNoiseDot: 0,
                        IsUseSimpleFont: true,
                        IsShowHollowLine: false,
                        IsShowNoiseDot: false,
                        IsShowNoiseText: false,
                        IsShowSlimeLine: false,
                        IsShowSineLine: false,
                        CaptchaLen: 6,
                        BgColor:{R:255, G:102, B:0, A:255}
                    },
                    ConfigDigit: {
                        Height: 80,
                        Width: 240,
                        CaptchaLen: 5,
                        MaxSkew: 0.7,
                        DotCount: 80
                    }
                },
            //输入框输入的数据
            form:{
                username: '',
                password: '',
                checkCode:'',
            },
            captcha:'',
            identifyCaptcha: '',
            remember:'',
        }

    },methods:{
        getCaptcha(){   //获取验证码函数
            var that = this
            that.$axios.post({
              url: apiurl.getCaptcha,
              data:that.captchaform
            }).then(res => {
              if(res.data.code==200){  //获取验证码成功
                  that.captcha=res.data.data
                  that.captchaform.Id = res.data.captchaId
              }else{
                that.$message.error(`获取验证码失败`)
              }
            })
        },
        submitForm(formName){
          var that = this
          that.$refs[formName].validate((valid) => {
            if (valid) {  //用户信息不能为空
              var logindata = {   //登录所需提交的表单
                AccountName : that.form.username,
                Password : this.$md5(that.form.password),    //密码md5加密
                Id : that.captchaform.Id,
                VerifyValue : that.form.checkCode
              }
            that.$axios.post({
              url: apiurl.login,
              data:logindata,
            }).then(res => {  
              if(res.data.code==200){  //登录成功
                   //如果选择了记住密码
                    if (document.querySelector("body > div > div.app > form > div:nth-child(3) > div > div.remember > label > input[type=checkbox]").checked==true){
                        that.remember = {
                            username: that.form.username,
                            password: that.form.password
                        }   
                        localStorage.setItem("remember",JSON.stringify(that.remember)) 
                    } 
                  that.$message.success(`登录成功`)
                  that.$router.push('/')
                  location.reload()
              }else{
                that.$message.error(res.data.msg)
                that.getCaptcha() //重置验证码
              }
            })
            }else {
                return false
              }
          })
        },register(){
           this.$router.push('/user/register')
        }
    }
}
</script>

<style scpoed> /*css局部生效*/
    .navbar{
      top: 0;
    }

    *{
    padding: 0;
    margin: 0;
    font-family: PingFangSC-Medium;
    -webkit-tap-highlight-color: rgba(255,255,255,0);
    }
    .bili-header-m {
    font: 12px 'Helvetica Neue', Helvetica, Arial, 'Microsoft Yahei', 'Hiragino Sans GB', 'Heiti SC', 'WenQuanYi Micro Hei', sans-serif;
    position: relative;
    z-index: 10000;
    background: #fff;
    margin: 0;
    padding: 0;
    }
    .top-banner {
    background: #00a0d8;
    height: 86px;
    text-align: center;
    margin-bottom: 20px;
    }
    a, img {
    border: none;
    }
    .title-line {
    width: 980px;
    height: 28px;
    margin: 0 auto;
    border-bottom: 1px solid #ddd;
    margin-bottom: 28px;
    text-align: center;
    }
    .title-line .tit {
    height: 56px;
    line-height: 56px;
    margin: 0 auto;
    padding: 0 20px;
    background: #fff;
    text-align: center;
    }
    .loginForm {
    width: 400px;
    margin: 0 auto;
    padding: 60px 0;
    }
    
    .identifyCaptcha {
    position: relative;
    top: 10px;
    width: 140px;
    height: 30px;
    border-radius: 2px;
    border: none;
    }
    
    .remember {
    margin: 10px 0;
    color: #717171;
    width: 410px;
    font-size: 12px;
    }
   
    .remember span {
    color: #bbb;
    margin-left: 10px;
    }



</style>